package cn.zyq.ykb.service.config.impl;

import cn.zyq.ykb.beans.user.User;
import cn.zyq.ykb.mapper.user.UserMapper;
import cn.zyq.ykb.system.common.BaseService;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.config.ConfigRecordMapper;
import cn.zyq.ykb.beans.config.ConfigRecord;
import cn.zyq.ykb.service.config.ConfigRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.List;

/**
 * 操作记录Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-05-24
 */
@Service
public class ConfigRecordServiceImpl  extends BaseService implements ConfigRecordService {

    @Autowired
    private ConfigRecordMapper configRecordMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询操作记录
     *
     * @param configRecordID 操作记录主键
     * @return 操作记录
     */
    @Override
    public ConfigRecord selectConfigRecordByConfigRecordID(Integer configRecordID) {
        ConfigRecord configRecord = configRecordMapper.selectByPrimaryKey(configRecordID);
        if (configRecord.getDeleted() == 1) {
            return null;
        }
        return configRecord;
    }

    /**
     * 查询操作记录
     *
     * @param serviceID 业务ID
     * @return 操作记录
     */
    @Override
    public ConfigRecord selectConfigRecordByServiceID(String serviceID) {
        ConfigRecord param = new ConfigRecord();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.configRecordMapper.selectOne(param);
    }

    /**
     * 查询操作记录列表
     *
     * @param pageParam
     * @return 操作记录
     */
    @Override
    public PageInfo<ConfigRecord> selectConfigRecordList(PageParam pageParam,User loginUser) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        pageParam.getKv().put("userID",loginUser.getUserID());
        List<ConfigRecord> list = this.configRecordMapper.list(pageParam.getKv());
        return new PageInfo<>(list);
    }

    /**
     * 查询操作记录列表
     *
     * @param param
     * @return 操作记录
     */
    @Override
    public List<ConfigRecord> selectConfigRecordList(ConfigRecord param) {
        param.setDeleted(0);
        return this.configRecordMapper.select(param);
    }
    /**
     * 新增操作记录
     *
     * @param param 操作记录
     * @return 结果
     */
    @Override
    public int insertConfigRecord(ConfigRecord param) {
        this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.configRecordMapper.insertSelective(param);
    }

    /**
     * 修改操作记录
     *
     * @param param 操作记录
     * @return 结果
     */
    @Override
    public int updateConfigRecord(ConfigRecord param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.configRecordMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除操作记录
     *
     * @param configRecordIDs 需要删除的操作记录主键
     * @return 结果
     */
    @Override
    public int deleteConfigRecordByConfigRecordIDs(Integer[] configRecordIDs) {
        int i = 0;
        for (Integer configRecordID : configRecordIDs) {
            i += this.deleteConfigRecordByConfigRecordID(configRecordID);
        }
        return i;
    }

    /**
     * 删除操作记录信息
     *
     * @param configRecordID 操作记录主键
     * @return 结果
     */
    @Override
    public int deleteConfigRecordByConfigRecordID(Integer configRecordID) {
        ConfigRecord configRecord = this.selectConfigRecordByConfigRecordID(configRecordID);
        if (configRecord == null) {
            return 0;
        } else {
            ConfigRecord storeInfo = new ConfigRecord();
            storeInfo.setConfigRecordID(configRecordID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.configRecordMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }

    @Override
    public List<ConfigRecord> getRecord(User loginUser) {
        List<ConfigRecord> record = this.configRecordMapper.getRecord(loginUser.getUserID());
        for (ConfigRecord configRecord : record) {
            User user = this.userMapper.selectByPrimaryKey(loginUser.getUserID());
            configRecord.setUser(user);
        }
        return record;
    }
}
